﻿#include "logindlg.h"
#include <QMessageBox>
//#pragma  execution_character_set("utf-8")
//#define  TR(text)     QString::fromUtf8(text)       
#define TR(text)      QString::fromLocal8Bit(text) 
LoginDlg::LoginDlg(QWidget *parent)
	: QDialog(parent) 
{
	isLogin = false;
	this->CreateLabel();
	this->CreateButton();
	this->CreateLineEdit();
	this->CreateLayout();
	this->SetDialogBackGround();
}

LoginDlg::~LoginDlg()
{

}

void LoginDlg::CreateLabel()
{
	m_lableUser = new QLabel(QString::fromLocal8Bit("用户名"));
	m_labelPasswd = new QLabel(QString::fromLocal8Bit("密码"));
	m_labelDatabase = new QLabel(QString::fromLocal8Bit("数据库名称"));
	m_labelIP = new QLabel("IP:");
}

void LoginDlg::CreateLineEdit()
{
	m_editUser = new QLineEdit();
	m_editPasswd = new QLineEdit();
	m_editPasswd->setEchoMode(QLineEdit::Password);  //设置成密码框
	m_editDatabase = new QLineEdit();
	m_editIp = new QLineEdit();
}

void LoginDlg::CreateButton()
{
	m_buttonLogin = new QPushButton(QString::fromLocal8Bit("登录"));
//	m_buttonLogin->setAutoFillBackground(true);
	m_buttonCanel = new QPushButton (QString::fromLocal8Bit("取消"));

	connect(m_buttonLogin, SIGNAL(clicked()), this, SLOT(Slot_Login()) );
	connect(m_buttonCanel, SIGNAL(clicked()), this, SLOT(Slot_LogOut()) );
}

void LoginDlg::CreateLayout()
{
	m_layout = new QGridLayout(this);

	m_layout->addWidget(m_lableUser, 0,0);
	m_layout->addWidget(m_editUser,0,1);

	m_layout->addWidget(m_labelPasswd, 1, 0);
	m_layout->addWidget(m_editPasswd, 1, 1);

	m_layout->addWidget(m_labelDatabase, 2, 0);
	m_layout->addWidget(m_editDatabase, 2,  1);

	m_layout->addWidget(m_labelIP, 3, 0);
	m_layout->addWidget(m_editIp, 3, 1);

	m_layout->addWidget(m_buttonLogin, 4, 0);
	m_layout->addWidget(m_buttonCanel, 4, 1);

	//设置列的宽度  设置两列一样宽
	m_layout->setColumnStretch(0, 1);
	m_layout->setColumnStretch(1, 1);

	m_layout->setMargin(5);
	//设置控件之间的间距
	m_layout->setSpacing(10);

	//设置layout一个最合算的尺寸  不容许随意更改大小
	m_layout->setSizeConstraint(QGridLayout::SetFixedSize);
}

void LoginDlg::SetDialogBackGround()
{
	//设置右上角的图标
	QIcon icon("2.png");
	this->setWindowIcon(icon);

	//填充背景颜色
	//1.要设置背景为自动填充
	this->setAutoFillBackground(true);
	QPalette palette;
	palette.setBrush(QPalette::Background, QBrush(QPixmap("bg.png")));
	setPalette(palette);
}



const QString& LoginDlg::GetUser()
{
	return this->m_strUser;
}

QString& LoginDlg::GetPasswd()
{
	return this->m_strPasswd;
}

QString& LoginDlg::GetDataBaseName()
{
	return this->m_strDataBase;
}

QString& LoginDlg::GetIp()
{
	return this->m_strIp;
}




//----------------------------------------------SLOT-----------------------------
void LoginDlg::Slot_Login()
{
	this->m_strUser = m_editUser->text();
	this->m_strPasswd = m_editPasswd->text();
	this->m_strDataBase = m_editDatabase->text();
	this->m_strIp = m_editIp->text();
	this->isLogin = true;
	close();
}

void LoginDlg::Slot_LogOut()
{
	//TODO::
	close();
}